﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SignUpLogin : DLL.MyWebPage
{
	private DLL.User.Component clsUserComp;
	private DLL.TrainingPerson.Component clsTrainingPersonComp;

	public SignUpLogin()
	{
		clsUserComp = new DLL.User.Component(Page);
		clsTrainingPersonComp = new DLL.TrainingPerson.Component(Page);
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			SetNoUserButtonText();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		BtnRegisterLogin.Click += new EventHandler(BtnRegisterLogin_Click);
		BtnSignUp.Click += new EventHandler(BtnSignUp_Click);
	}

	/// <summary>
	/// 没有用户名用户报名按钮按下事件响应函数
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void BtnSignUp_Click(object sender, EventArgs e)
	{
		int nTrainingID = 0;
		string strType = "";

		strType = Request["Type"].ToString();
		nTrainingID = Convert.ToInt32(Request["TrainingID"]);

		if (strType == "Apply")
		{
			if (clsTrainingPersonComp.TrainingApply(nTrainingID, TbJobNumber.Text, TbName.Text, TbIDCard.Text))
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "报名成功！");
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "报名失败！请检查输入信息或联系培训组织部门");
			}
		}
		else
		{
			DLL.User.Entity clsUser = new DLL.User.Entity();

			if (clsUserComp.CheckNoUserLogin(TbJobNumber.Text, TbName.Text, TbIDCard.Text, ref clsUser, nTrainingID))
			{
				Session["UserID"] = clsUser.ID;
				Session["ColumnIDs"] = clsUser.ColumnIDs;
				Session["WebColumnIDs"] = clsUser.WebColumnIDs;
				Session["DepartmentID"] = clsUser.DepartmentID;
				//Response.Redirect("~/Admin/Main.aspx");
				Literal litMessage = new Literal();

				litMessage.Text = "<script>window.open('Admin/Main.aspx')</script>";
				Page.Controls.Add(litMessage);
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "信息输入错误或者您没有报名参加培训，无法学习课程！");
			}
		}
	}

	/// <summary>
	/// 已有用户名用户登录按钮按下事件响应函数
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void BtnRegisterLogin_Click(object sender, EventArgs e)
	{
		CheckUserLogin();
	}

	/// <summary>
	/// 检查用户名和密码登录系统
	/// </summary>
	/// <returns></returns>
	private void CheckUserLogin()
	{
		DLL.User.Entity clsUser = new DLL.User.Entity();

		if (clsUserComp.CheckLogin(TbUsername.Text, TbPassword.Text, ref clsUser))
		{
			Session["UserID"] = clsUser.ID;
			Session["ColumnIDs"] = clsUser.ColumnIDs;
			Session["WebColumnIDs"] = clsUser.WebColumnIDs;
			Session["DepartmentID"] = clsUser.DepartmentID;
			//Response.Redirect("Admin/Main.aspx");
			//Server.Transfer("~/Admin/Main.aspx");
			//Response.Write("<script>window.open('Admin/Main.aspx', 'newwindow')</script>");
			Literal litMessage = new Literal();

			litMessage.Text = "<script>window.open('Admin/Main.aspx')</script>";
			Page.Controls.Add(litMessage);
		}
		else
		{
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
		}
	}

	/// <summary>
	/// 根据是报名还是学习显示按钮文本
	/// </summary>
	private void SetNoUserButtonText()
	{
		string strType = "";

		strType = Request["Type"].ToString();

		if (strType == "Apply")
		{
			BtnSignUp.Text = "报名";
		}
		if (strType == "Learn")
		{
			BtnSignUp.Text = "学习";
		}
	}
}